﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Rent;

namespace Jungle.Alex.WebControls
{
    public class BottomOffers : WebControl
    {
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            Offer offs = new Offer();
            offs.GetRnd("15");
            foreach (Offer o in offs.OfferList)
            {
                string imgUrl = UrlHelper.GetSiteUrl();
                int topMove = 0;
                int leftMove = 0;
                if (o.Photo.Length > 10)
                {
                    string[] ff = new string[1];
                    string fn = "";
                    if (o.Photo.IndexOf(",") != -1)
                    {
                        ff = o.Photo.Split(',');
                        imgUrl += ff[1];
                        fn = ff[1];
                    }
                    else
                    {
                        ff[0] = o.Photo;
                        imgUrl += ff[0];
                        fn = ff[0];
                    }
                    System.Drawing.Image img = System.Drawing.Image.FromFile(Context.Server.MapPath(fn));
                    if (img.Height > 115) topMove = (-1) * (img.Height - 115) / 2;
                    if (img.Width > 115) leftMove = (-1) * (img.Width - 115) / 2;
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/offers.aspx?cmd=show&id=" + o.OfferId.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute(HtmlTextWriterAttribute.Title, GetShortDescription(o));
                    writer.AddAttribute("class", "rndItem");
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, imgUrl);
                    writer.AddStyleAttribute("background-position", leftMove.ToString() + "px " + topMove.ToString() + "px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.RenderEndTag();
                writer.RenderEndTag();
            }
            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public string GetShortDescription(Offer o)
        {
            string result = "";
            string a0 = "";

            if (o.OfferType) a0 = "Продается "; else a0 = "Сдаеться в аренду ";
            if(o.ObjectType == 4)
                result = a0 + " гараж";
            if (o.ObjectType == 5)
                    result = a0 + " участок земли";
            if (o.ObjectType < 4)
            result = a0 + Offer.ddl_UnderType(o.ObjectType).Items.FindByValue(o.UnderTypeId.ToString()).Text;
            if(o.CityId > 0)
            {
                result += " \r\n " + Offer.ddl_City().Items.FindByValue(o.CityId.ToString()).Text + " " + o.District + " " + o.Street + " " + o.HomeNumber;
            }
            result += "\r\nОбъявление от: " + o.PubDate;
            if (o.AutorType == 0) result += ", автор объявления: хозяин \r\n";
            else result += ", автор объявления: агенство \r\n";

            int lenght = 160;
            if (o.Description.Length < lenght) lenght = o.Description.Length;
            result += o.Description.Substring(0, lenght);
            return result;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "115");
            writer.AddAttribute(HtmlTextWriterAttribute.Height, "115");
            writer.AddAttribute("style", "border: 2px solid #fff;");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
    }
}
